/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package genHeatMap;

import commonLibs.GroupFireSaves;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;
import javax.swing.SwingWorker;
import modelNetGen.FilesCommons;
import commonLibs.ModelType;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

/**
 *
 * @author Libra
 */
public class HeatMapUI extends javax.swing.JFrame {

    /**
     * Creates new form HeatMapUI
     */
    public HeatMapUI() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnDefaultFolder = new javax.swing.JButton();
        lblFileCount = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtLog = new javax.swing.JTextArea();
        jPanel3 = new javax.swing.JPanel();
        btnRun = new javax.swing.JButton();
        txtTimeWindow = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        txtPopulationProportion = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtSaveFile = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        btnDefaultFolder.setText("Default Folder");
        btnDefaultFolder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDefaultFolderActionPerformed(evt);
            }
        });

        lblFileCount.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        lblFileCount.setText("0/0");

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnDefaultFolder)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(174, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(lblFileCount)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDefaultFolder)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
                .addComponent(lblFileCount)
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        txtLog.setColumns(20);
        txtLog.setRows(5);
        jScrollPane1.setViewportView(txtLog);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        btnRun.setText("Run");
        btnRun.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRunActionPerformed(evt);
            }
        });

        txtTimeWindow.setText("20");
        txtTimeWindow.setPreferredSize(new java.awt.Dimension(40, 20));

        jLabel1.setText("ms");

        txtPopulationProportion.setText("1");
        txtPopulationProportion.setPreferredSize(new java.awt.Dimension(40, 20));

        jLabel2.setText("%");

        txtSaveFile.setText("save");
        txtSaveFile.setPreferredSize(new java.awt.Dimension(60, 20));

        jLabel3.setText("Save File Name");

        jLabel4.setText(".txt");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnRun)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(txtTimeWindow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(txtPopulationProportion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtSaveFile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)))
                .addContainerGap(59, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTimeWindow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(txtPopulationProportion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(txtSaveFile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addGap(20, 20, 20)
                .addComponent(btnRun)
                .addContainerGap(63, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnDefaultFolderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDefaultFolderActionPerformed
        fileList = new ArrayList<>();
        File dir = new File(FilesCommons.getJarFolder(""));
        txtLog.append("Open folder " + dir.getAbsolutePath() + "\n");
        File[] files = dir.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.toLowerCase().endsWith("_fire.ser");
            }
        });
        for (File f : files) {
            fileList.add(f.getAbsolutePath());
        }
        if (fileList.isEmpty()) {
            txtLog.append("No ser files found in the folder!\n");
        }
        lblFileCount.setText("0/" + fileList.size());
    }//GEN-LAST:event_btnDefaultFolderActionPerformed

    private void btnRunActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRunActionPerformed
        new SwingWorker<Void, Integer>() {
            @Override
            protected Void doInBackground() {
                int timeWindow = Integer.parseInt(txtTimeWindow.getText());
                int populationProportion = Integer.parseInt(txtPopulationProportion.getText());
                HashMap<String, StructResult> results = new HashMap<>();
                for (String pathToFile : fileList) {
                    try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(pathToFile))) {
                        GroupFireSaves save = (GroupFireSaves) in.readObject();
                        String hashString = save.getHashString();
                        StructResult result = flexAdd(hashString, results);
                        result.add(save.getType(), save.getConnProb(), save.getWeightScale(), save.getPopulationFireFreq(timeWindow, populationProportion));
                    } catch (Throwable ex) {
                        System.out.println(ex.toString());
                    }
                }
                for (String hashKey : results.keySet()) {
                    StructResult r = results.get(hashKey);
                    r.sort();
                    record(r);
                }
                return null;
            }

            private void record(StructResult r) {
                String saveFile = modelNetGen.FilesCommons.getJarFolder("") + "\\" + txtSaveFile.getText() + ".txt";
                try (BufferedWriter w = new BufferedWriter(new FileWriter(saveFile, true))) {

                    log("Batch\t" + r.getHashString(), w);
                    log("ConnProbs (Row) : " + Arrays.toString(r.getConnProbs()), w);
                    log("Weight Scales (Column) : " + Arrays.toString(r.getWeightScales()), w);
                    logArray("Control", r.getCtrlFireArray(), w);
                    logArray("Model", r.getExpFireArray(), w);
                } catch (Throwable ex) {
                    System.out.println(ex.toString());
                }
            }

            private void logArray(String subtitle, Float[][] array, BufferedWriter w) throws IOException {
                log(subtitle, w);
                for (Float[] row : array) {
                    for (Float column : row) {
                        txtLog.append(Float.toString(column) + "\t");
                        w.write(Float.toString(column) + "\t");
                    }
                    log("", w);
                }
//                commonLibs.WriteFile.writeMatrix(txtSaveFile.getText() + ".txt", array);
            }

            @Override
            protected void process(List<Integer> chunks
            ) {
                lblFileCount.setText(chunks.get(chunks.size() - 1).toString()
                        + "/" + fileList.size());
            }
        }
                .execute();

    }//GEN-LAST:event_btnRunActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        (new TEST()).test();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void log(String s, BufferedWriter w) throws IOException {
        txtLog.append(s + "\n");
//        commonLibs.WriteFile.writeString(txtSaveFile.getText() + ".txt", s);
        w.write(s + "\n");
    }

    private StructResult flexAdd(String hashString, HashMap<String, StructResult> map) {
        if (map.containsKey(hashString)) {
            return map.get(hashString);
        } else {
            map.put(hashString, (new StructResult(hashString)));
            return map.get(hashString);
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(HeatMapUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new HeatMapUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDefaultFolder;
    private javax.swing.JButton btnRun;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblFileCount;
    private javax.swing.JTextArea txtLog;
    private javax.swing.JTextField txtPopulationProportion;
    private javax.swing.JTextField txtSaveFile;
    private javax.swing.JTextField txtTimeWindow;
    // End of variables declaration//GEN-END:variables
    private List<String> fileList;
//    private List<popFireFreq> popFireList;

    private class StructResult {

        final private List<popFireFreq> expResultList;
        final private List<popFireFreq> ctrlResultList;
        final private HashMap<Float, Integer> connProbMap;
        final private HashMap<Float, Integer> weightScaleMap;
        final private String hashString;
        final private TreeSet<Float> weights;
        final private TreeSet<Float> connProbs;
        private String row;

        public StructResult(String hashString) {
            this.expResultList = new ArrayList<>();
            this.ctrlResultList = new ArrayList<>();
            this.hashString = hashString;
            weights = new TreeSet<>();
            connProbs = new TreeSet<>();
            connProbMap = new HashMap<>();
            weightScaleMap = new HashMap<>();
        }

        public void add(ModelType type, float connProb, float weightScale, float popFireFreq) {
            (type == ModelType.Network ? expResultList : ctrlResultList).add(new popFireFreq(type, connProb, weightScale, popFireFreq));
            connProbs.add(connProb);
            weights.add(weightScale);
        }

        public String getHashString() {
            return hashString;
        }

        public Float[] getConnProbs() {
            return connProbs.toArray(new Float[connProbs.size()]);
        }

        public Float[] getWeightScales() {
            return weights.toArray(new Float[weights.size()]);
        }

        public void sort() {
            //Construct lookuptable;
            row = hashString + "\t";
            Iterator<Float> iter = connProbs.iterator();
            for (int i = 0; iter.hasNext(); i++) {
                float value = iter.next();
                connProbMap.put(value, i);
                row += Float.toString(value) + "\t";
            }
            iter = weights.iterator();
            for (int i = 0; iter.hasNext(); i++) {
                weightScaleMap.put(iter.next(), i);
            }
        }

        private Float[][] getFireArray(List<popFireFreq> list) {
            Float[][] freqs = new Float[connProbs.size()][weights.size()];
            for (popFireFreq pop : list) {
                freqs[connProbMap.get(pop.getConnProb())][weightScaleMap.get(pop.getWeightScale())] = pop.getPopFireFreq();
            }
            return freqs;
        }

        public Float[][] getCtrlFireArray() {
            return getFireArray(ctrlResultList);
        }

        public Float[][] getExpFireArray() {
            return getFireArray(expResultList);
        }

        private class popFireFreq {

            final private float connProb;
            final private float weightScale;
            final private float popFireFreq;
            final private ModelType type;

            public popFireFreq(ModelType type, float connProb, float weightScale, float popFireFreq) {
                this.type = type;
                this.connProb = connProb;
                this.weightScale = weightScale;
                this.popFireFreq = popFireFreq;
            }

            public float getConnProb() {
                return connProb;
            }

            public float getWeightScale() {
                return weightScale;
            }

            public float getPopFireFreq() {
                return popFireFreq;
            }
        }

    }

}
